<?php
define('TS_ROOT', './');
require(TS_ROOT . 'common.php');
require(INC_DIR . 'bbcode.php');

$start  = isset($_GET['start']) ? abs(intval($_GET['start'])) : 0;
$blog_id = isset($_GET[POST_BLOG_URL]) ? (int) $_GET[POST_BLOG_URL] : 0;
$submit   = !empty($_POST['submit']);
$mode = (string) @$_REQUEST['mode'];

if ($mode == 'smilies')
{
	generate_smilies('window');
	exit;
}

if ($ts_cfg['blog_enabled'])
{
	$datastore->enqueue('blog_show');
}

$user->session_start();

if ($blog_id)
{
	$sql = "SELECT b.*
	FROM ". TS_BLOGS            ." b
	WHERE b.blog_id = $blog_id
	";
}

if (!$t_data = DB()->fetch_row($sql))
{
	meta_refresh('index.php' , 10);
	ts_die($lang['TOPIC_POST_NOT_EXIST']);
}

$blog_has_poll = ($t_data['blog_vote'] && !IS_GUEST);
$poll_time_expired = ($t_data['blog_time'] < TIMENOW - $ts_cfg['poll_max_days']*86400);
$can_manage_poll = ($t_data['blog_poster_id'] == $userdata['user_id']);
$can_add_poll = ($can_manage_poll && !$blog_has_poll && !$poll_time_expired && !$start);

//
// Send vars to template
//
$template->assign_vars(array(
	'BLOG_ID'            => $blog_id,
	'BLOG_TITLE'   		 => $t_data['blog_title'],
	'BLOG_TEXT'	   		 => bbcode2html($t_data['blog_text']),
	'BLOG_POSTER'  		 => $t_data['blog_poster'],
	'BLOG_TIME'	   		 => bb_date($t_data['blog_time']),
	
	'BLOG_HAS_POLL'       => $blog_has_poll,
	'POLL_IS_EDITABLE'    => (!$poll_time_expired),
	'POLL_IS_FINISHED'    => ($t_data['blog_vote'] == POLL_FINISHED),
	'CAN_MANAGE_POLL'     => $can_manage_poll,
	'CAN_ADD_POLL'        => $can_add_poll,
));

//
// Does this topic contain a poll?
//
if ($blog_has_poll)
{
	$poll_votes_js = get_poll_data_items_js($blog_id);
	if (!$poll_votes_js)
	{
		$template->assign_vars(array(
			'BLOG_HAS_POLL' => false,
		));
	}
	else
	{
		$template->assign_vars(array(
			'SHOW_VOTE_BTN' => poll_is_active($t_data),
			'POLL_VOTES_JS' => $poll_votes_js,
		));
	}
}

// Коментарии
$sql_comment = DB()->fetch_rowset("SELECT 
					bc.*, b.enabled_comment,
					u.user_id, u.username, u.user_rank, u.avatar_ext_id
						FROM ". TS_BLOG_COMMENTS ." bc 
						LEFT JOIN ". TS_BLOGS ." b ON(bc.blog_id 			= b.blog_id) 
						LEFT JOIN ". TS_USERS ." u ON(bc.comment_user_id 	= u.user_id)
					WHERE 
						bc.blog_id = {$blog_id}
					ORDER BY 
						bc.comment_time ASC");

						
foreach($sql_comment as $rows)
{
	$template->assign_block_vars('comment', array(
		'BLOG_ID'			=> $rows['blog_id'],
		'COMMENT_ID'		=> $rows['comment_id'],
		'COMMET_TIME'		=> bb_date($rows['comment_time']),
		'COMMENT_TEXT'		=> bbcode2html($rows['comment_text']),
		'AVATAR_LINK'		=> PROFILE_URL . $rows['user_id'],
		'COMMENT_USER'		=> profile_url($rows),
		'COMMENT_AVATAR'	=> get_avatar($rows['user_id'], $rows['avatar_ext_id']),
	));
}

if($submit)
{
	if (bf($userdata['user_opt'], 'user_opt', 'dis_post'))
	{
		ts_die('Вам запрещено оставлять свои коментарии в темах. Обратитесь к администртора форума.');
	}
	
	$subject 	= (string) $_POST['text'];
	
	$template->assign_vars(array(
		'ACTION'			=> $comment->action_submit($blog_id, $subject),
	));
	$comment->update_stats($userdata['user_id']);
}

// Show blog index
if ($ts_cfg['blog_enabled'])
{
	if (!$blog_show = $datastore->get('blog_show'))
	{
		$datastore->update('blog_show');
		$blog_show = $datastore->get('blog_show');
	}
	
	$template->assign_vars(array(
		'SHOW_BLOG_NEWS' => true,
	));
	
	foreach($blog_show as $show)
	{
		$template->assign_block_vars('show', array(
			'SHOW_BLOG_ID' 		=> $show['blog_id'],
			'SHOW_LINK'			=> BLOG_URL . $show['blog_id'],
			'SHOW_TITLE'    	=> str_short($show['blog_title'], $ts_cfg['max_show_blog_title']),
			'PRE_TITLE'			=> $show['blog_title'],
			'SHOW_TIME'     	=> $show['blog_time'],
		));
	}
}


// Generate smilies listing for page output
generate_smilies('inline');

print_page('blog.tmpl');